import config from '@/js/conf/Config.js';
import staticCont from '@/js/const/StaticConst.js';
import cmd from "@/js/Cmd.js";
import store from '@/store'
import md5 from "../lib/md5.js";

import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
let that = Vue.prototype;

config.setHost();

let net = {};
net.host = config.host;
net.payHost = config.payHost;

net.request = async function ({
	method = "POST",
	url,
	data,
	showLoading = true,
	showToast = true,
	error,
	msg = false,
	print = false,
	app = "pc",
	payKey = ""
}) {
	if (showLoading) {

		// uni.showLoading({
		// 	title: '加载中...',
		// 	mask: true
		// });

		Vue.prototype.$loadingGlobal.show()
	}
	let user = that.$store.state.user;
	// data.app = staticCont.CLIENT_TYPE;
	data.is_ajax = staticCont?.AJAX_TYPE;
	// if (user && user.token && !paySkey) // 支付不用token
	// {
	// 	data.token = user.token;
	// }
	data.token = uni.getStorageSync('token') || '';
	if (app){
		data.app = app;
	}
	let debug = '';
	if (data.hasOwnProperty('debug'))
		debug = "?debug=debug";
	if (!print) {
		if (payKey != "") {
			url = net.payHost + url + debug;
			let d = {};
			Object.keys(data).sort().forEach(function (key) {
				d[key] = data[key];
			});
			// console.log(JSON.stringify(d));
			let sign = "";
			for (let k in d) {
				sign += "&" + k + "=" + d[k];
			}
			sign = payKey + sign + "&" + payKey;
			// console.log("net.request(),sign================:", sign);
			data.sign = md5.hex_md5(sign);
			// console.log("net.request(),data.sign================:", data.sign);
		} else {
			url = net.host + url + debug;
		}
	} else {
		url = net.host + url;
	}

	var req = {
		url: url,
		method: method,
		data: data,
		withCredentials: true, // 是否允许携带cookie
	}
	// console.log("user=url=xx=", url);
	let isLogout = false;
	if (url.includes("auth/logout")) {
		isLogout = true;
	} else {
		isLogout = false;
	}
	let header = {
		'content-type': 'application/json'
	};
	// console.log("url=:", url, "data=:", JSON.stringify(data));
	let [e, res] = await uni.request(req, header);
	// console.log("url=:", url, "网络请求:", req, "网络返回:", res);
	if (showLoading) {
		// uni.hideLoading();
		Vue.prototype.$loadingGlobal.hide()
	}
	if (!res && !isLogout) {
		uni.showToast({
			title: "网络请求异常，请检查网络！",
			icon: 'none',
			duration: 3000,
		});
		return null;
	};
	if (e) {
		if (error != undefined) {
			error(res)
		}
		return null;
	}
	// res.data.status = -1000;
	// alert(JSON.stringify(res));
	if (res.data.status != 1) {
		if (res.data.status == -999) {
			//添加渠道商，添加店员工检测异常
			return res.data;
		}
		if (res.data.msg && showToast) {
			uni.showToast({
				title: res.data.msg,
				icon: 'none',
				duration: 3000,
			});
		}

		if (res.data.status == -888) {
			//要求授权
			// console.log("net授权:", res.data.status);
			return res.data;
		}

		if (res.data.status == -1000) {
			//重新登录
			cmd.external(cmd.CMD_LOGOUT); //回到登录窗口
			uni.reLaunch({
				url: '/pages/login/login?reLogin=true'
			})
			return;
		}
	}
	return res.data;
};
export default net;